#include <stdio.h>
#include <kewinternal.h>
#include "myglib.h"

#define SOURCE_FILE_NAME "libkew:message.c"

#define message_receiver(OBJECT) ((OBJECT)->context[0])
#define message_selector(OBJECT) ((OBJECT)->context[1])
#define message_arguments(OBJECT) ((OBJECT)->context[2])

METHOD(Receiver)
{
  results[0] = message_receiver(receiver);
}

METHOD(Selector)
{
  results[0] = message_selector(receiver);
}

METHOD(Arguments)
{
  results[0] = message_arguments(receiver);
}

static KewObjectType kew_message_type = {
  SOURCE_FILE_NAME, __LINE__,
  3,
  {
    {"receiver", Receiver, 0, 1},
    {"selector", Selector, 0, 1},
    {"arguments", Arguments, 0, 1},
    {NULL, NULL, 0, 0}
  }
};

KewObject *
KewNewMessage(KewObject *receiver,
              KewObject *selector,
              KewObject *arguments)
{
  KewObject *new_message = KewNewObject(&kew_message_type);
  message_receiver(new_message) = receiver;
  message_selector(new_message) = selector;
  message_arguments(new_message) = arguments;
  return new_message;
}

KewObject *
KewNewMessageRaw(KewObject *receiver,
                 char *selector,
                 KewObject *args[],
                 unsigned args_size)
{
  KewObject *selector_object = KewNewString(selector);
  KewObject *args_object = KewNewListFromArray(args, args_size);
  return KewNewMessage(receiver, selector_object, args_object);
}

void
Kew__InitMessage(void)
{
}
